<?php
/**
 * Created by 张彪.
 * User: 小马出行
 * Date: 2018/8/3
 * Time: 17:37
 */
namespace app\index\logic;
use think\Db;
use think\Model;

class LocalLogic extends Model
{
    public $map = array('inner'=>array('auth_status'=>1),'inter'=>array('auth_status'=>1));
    public $field_inner = "d.driver_id,driver_name,car_plate,car_model,car_brand,color";
    public $field_inter = "d.id as driver_id,driver_name,car_plate,car_model,car_brand,car_color as color";
    /**
     * 获取某位司机坐标
     * @param $map
     * @param $field
     * @return  mixed
     * */
    public function getDriverCoordinate($map,$field='*')
    {
        return Db::connect(config('mongodb'))->name('driver_location')->where($map)->field($field)->find();
    }

    /**
     * 获取某位司机某一天的行程路径
     */
    public function getTrajectory()
    {

    }

    /**
     * 统计司机数量
     * @param $map
     * @return  array
     * */
    public function statistics($map = array())
    {
        $trip_inner = $trip_inter  = $taking_inner = $taking_inter = $offline_inner = $offline_inter = 0;
        if($map['inner']){
            $driver_id = implode(',',$map['inner']);
            $sql_taking = "SELECT COUNT(*) AS amount FROM xm_driver WHERE driver_id IN ($driver_id) AND driver_status = 1";
            $sql_trip = "SELECT COUNT(*) AS amount FROM xm_driver WHERE driver_id IN ($driver_id) AND driver_status IN (2,3,4)";
            $sql_offline = "SELECT COUNT(*) AS amount FROM xm_driver WHERE driver_id IN ($driver_id) AND driver_status IN (0,5)";
            $taking_inner = Db::query($sql_taking)[0]['amount'];
            $trip_inner = Db::query($sql_trip)[0]['amount'];
            $offline_inner = Db::query($sql_offline)[0]['amount'];
        }
        if($map['inter']){
            $driver_id = implode(',',$map['inter']);
            $sql_taking = "SELECT COUNT(*) AS amount FROM inter_driver WHERE id IN ($driver_id) AND is_goon = 2";
            $sql_trip = "SELECT COUNT(*) AS amount FROM inter_driver WHERE id IN ($driver_id) AND is_goon IN (1,3)";
            $sql_offline = "SELECT COUNT(*) AS amount FROM inter_driver WHERE id IN ($driver_id) AND is_goon = 4";
            $taking_inter = Db::query($sql_taking)[0]['amount'];
            $trip_inter = Db::query($sql_trip)[0]['amount'];
            $offline_inter = Db::query($sql_offline)[0]['amount'];
        }
        return array('trip'=>$trip_inner+$trip_inter, 'taking'=>$taking_inner+$taking_inter,'offline'=>$offline_inner+$offline_inter);
    }

    /**
     * 处理司机坐标信息
     * @param $local //司机坐标信息
     * @param $field //字段信息
     * @return array|boolean //司机坐标信息
     * */
    public function dealDriverLocation($local,$field=array())
    {
        if(empty($local)) return false;
         $where_inner = $where_inter = $data = array();
        /************************************************城内************************************************/
        if($local['inner_driver_id']){
            $where_inner['record_status'] = 2;
            $where_inner['d.driver_id'] = array('in',$local['inner_driver_id']);
            $inner_driver = $this->searchInnerDriver($where_inner,$field['inner']);
            if($inner_driver){
                $inner_driver = $this->dealDriverInfo($inner_driver);unset($inner_driver['driver_id']);
                $inner_driver = $this->dealDriverInfoLocation($inner_driver,$local['inner']);
                $data = array_merge($data,$inner_driver); //$data = decode_array_2($data);
            }else{
                $data = array_merge($data,$local['inner']);
            }
        }
        /************************************************城际************************************************/
        if($local['inter_driver_id']){
            $where_inter['d.id'] = array('in',$local['inter_driver_id']);
            $inter_driver = $this->searchInterDriver($where_inter,$field['inter']);
            if($inter_driver){
                $inter_driver = $this->dealDriverInfo($inter_driver);unset($inter_driver['driver_id']);
                $inter_driver = $this->dealDriverInfoLocation($inter_driver,$local['inter']);
                $data = array_merge($data,$inter_driver);
            }else{
                $data = array_merge($data,$local['inter']);
            }
        }
        $return['sta'] = $this->statistics(array('inner'=>$local['inner_driver_id'],'inter'=>$local['inter_driver_id']));
        $return['sta']['total'] = count($data);
        $return['data'] = $data;
        return $return;
    }

    /**
     * 搜索城内司机
     * @param $map
     * @param $field
     * @param $flag //当map含有region_code时，true为城市，false为县区
     * @return  mixed
     * */
    public function searchInnerDriver($map,$field='*',$flag=true)
    {
        return $flag ? Db::name('driver d')->join('region r','d.work_city=r.region_id','LEFT')
            ->join('car_info ci','ci.driver_id=d.driver_id','LEFT')->where($map)
            ->field($field)->select():
            Db::name('driver d')->join('region r','d.work_city=r.parent_id','LEFT')
                ->join('car_info ci','ci.driver_id=d.driver_id','LEFT')->where($map)
                ->field($field)->select();
    }

    /**
     * 搜索城际司机
     * @param $map
     * @param $field
     * @param $flag //当map含有region_code时，true为城市，false为县区
     * @return  mixed
     * */
    public function searchInterDriver($map,$field='*',$flag=true)
    {
        return $flag ? Db::table('inter_driver d')->join('region r','d.work_city=r.region_id','LEFT')
            ->join(config('intercity_db')['database'].'.inter_car_list cl','cl.driver=d.id','LEFT')
            ->where($map)->field($field)->select():
            Db::table('inter_driver d')->join('region r','d.work_city=r.parent_id','LEFT')
                ->join(config('intercity_db')['database'].'.inter_car_list cl','cl.driver=d.id','LEFT')
                ->where($map)->field($field)->select();
    }

    /**
     * 处理查询出来的司机信息
     * @param $driver_info //司机信息
     * @return array
     * */
    public function dealDriverInfo($driver_info)
    {
        if(empty($driver_info)){
            return array('driver_id'=>array());
        }
        foreach ($driver_info as $key => $val){
            $val['driver_id'] = (int)$val['driver_id'];
            $val['driver_status'] = (int)$val['driver_status'];
            $driver_info['driver_id'][] = $val['driver_id'];
            unset($driver_info[$key]);
            $driver_info[$val['driver_id']] = $val;
        }
        return $driver_info;
    }

    /**
     * 融合司机信息和司机坐标信息
     * @param $driver_info //司机信息
     * @param $local //司机坐标信息
     * @return  boolean|array
     * */
    public function dealDriverInfoLocation($driver_info,$local)
    {
        if(empty($local) || empty($driver_info)){
            return false;
        }
        foreach ($local as $key => $val){
            $driver_info[$val['driver_id']]&&$local[$key] = array_merge($val,$driver_info[$val['driver_id']]);
        }
        return $local;
    }

    /**
     * 城内订单信息
     * */
    public function getInnerOrderInfo($map,$field='*')
    {
        return Db::name('order_common')->where($map)->field($field)->find();
    }

    /**
     * 城际订单信息
     * */
    public function getInterOrderInfo($map,$field='*')
    {
        return Db::table('inter_order_common')->where($map)->field($field)->select();
    }
}